#include "pageinterface.h"
#include "ui_pageinterface.h"

PageInterface::PageInterface(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageInterface)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    btnlist.append(ui->btn_1);
    btnlist.append(ui->btn_2);
    btnlist.append(ui->btn_3);
    btnlist.append(ui->btn_4);
    btnlist.append(ui->btn_5);
    btnlist.append(ui->btn_6);
    btnlist.append(ui->btn_7);
    QSignalMapper *sm = new QSignalMapper(this);
    for(int i=0; i<btnlist.size(); ++i)
    {
        connect(btnlist.at(i), SIGNAL(clicked()), sm, SLOT(map()));
        sm->setMapping(btnlist.at(i), i);
    }
    connect(sm, SIGNAL(mapped(int)), SLOT(slotBtnClicked(int)));

    QStringList header1 = QStringList() << "No" << "信号" << "C1" << "C4";
    QStringList header2 = QStringList() << "No" << "信号" << "C2" << "C3";
    QStringList header3 = QStringList() << "No" << "信号" << "Tc1" << "Mp1" << "M1" << "M2"<< "Mp2" << "Tc2";
    QString headerStyle = "QHeaderView::section{ background-color:black; color:white; height:44; font-size:14pt; border: 1px solid white }";
    tablelist.append(ui->tableWidget_1);
    tablelist.append(ui->tableWidget_2);
    tablelist.append(ui->tableWidget_3);
    tablelist.append(ui->tableWidget_4);
    tablelist.append(ui->tableWidget_5);
    tablelist.append(ui->tableWidget_6);
    tablelist.append(ui->tableWidget_7);
    for(int i=0; i<tablelist.size(); ++i)
    {
        tablelist[i]->setSelectionMode(QAbstractItemView::NoSelection);
        tablelist.at(i)->horizontalHeader()->setStyleSheet(headerStyle);
        tablelist.at(i)->horizontalHeader()->setDisabled(true);
        tablelist.at(i)->horizontalHeader()->setFont(QFont("arial"));
        tablelist.at(i)->setFont(QFont("arial"));

        tablelist.at(i)->setHorizontalHeaderLabels(header3);
        tablelist.at(i)->setColumnWidth(0, 120);
        tablelist.at(i)->setColumnWidth(1, 386);
        tablelist.at(i)->setColumnWidth(2, 110);
        tablelist.at(i)->setColumnWidth(3, 110);
        tablelist.at(i)->setColumnWidth(4, 110);
        tablelist.at(i)->setColumnWidth(5, 110);
        tablelist.at(i)->setColumnWidth(6, 110);
        tablelist.at(i)->setColumnWidth(7, 110);


        for(int k=0; k<tableRowCount; ++k)
        {
            for(int j=0; j<tableColumnCount; ++j)
            {
                QTableWidgetItem *item = new QTableWidgetItem("");
                if(j != 1)
                    item->setTextAlignment(Qt::AlignCenter);
                tablelist.at(i)->setItem(k, j, item);
            }
        }
    }
    initTable();
    slotBtnClicked(0);
}

PageInterface::~PageInterface()
{
    delete ui;
}

void PageInterface::initTable()
{
    for(int i=0; i<IO_TYPE_CNT; ++i)
    {
        QList<int> indexlist;
        for(int j=0; j<GlobalConfig::interfaceList[i].size(); ++j)
        {
            int index = GlobalConfig::interfaceList[i].at(j).index;
            if(indexlist.contains(index))
                continue;
            indexlist.append(index);
            QString pin = GlobalConfig::interfaceList[i].at(j).pinId;
            QString name = GlobalConfig::interfaceList[i].at(j).description;
            pinlist[i].append(pin);
            namelist[i].append(name);
        }
        totalpage.append((pinlist[i].size()-1)/tableRowCount+1);
    }
    tableIndex = 1;
    ui->totalPage->setText(QString().setNum(totalpage.at(tableIndex)));
    updateTablePage();
}

void PageInterface::updateTablePage()
{
    int page = ui->currentPage->text().toInt();
    for(int i=0; i<tableRowCount; ++i)
    {
        tablelist[tableIndex]->item(i, 0)->setFont(QFont("arial", 15));
        for(int j=0; j<tableColumnCount; ++j)
            tablelist[tableIndex]->item(i, j)->setText(QString(""));

        int idx = tableRowCount*(page-1)+i;
        if(idx < namelist[tableIndex].size())
        {
            tablelist[tableIndex]->item(i, 0)->setText(pinlist[tableIndex].at(idx));
            tablelist[tableIndex]->item(i, 1)->setText(namelist[tableIndex].at(idx));
        }
    }
    for(int i=0; i<GlobalConfig::interfaceList[tableIndex].size(); ++i)
    {
        int row = GlobalConfig::interfaceList[tableIndex][i].index;
        if(row>=tableRowCount*(page-1) && row<=(tableRowCount*page-1))
        {
            int col = GlobalConfig::interfaceList[tableIndex][i].car + 1;
            int val = GlobalConfig::interfaceList[tableIndex][i].value;
            tablelist[tableIndex]->item(row%tableRowCount, col)->setText(QString().setNum(val>0x7FFF ? (val-0xFFFF-1) : val));
        }
    }
}

void PageInterface::slotBtnClicked(int index)
{
    if(tableIndex == index)
    {
        btnlist.at(index)->setChecked(true);
        return;
    }
    btnlist.at(tableIndex)->setChecked(false);
    tableIndex = index;
    btnlist.at(tableIndex)->setChecked(true);
    ui->stackedWidget->setCurrentIndex(tableIndex);
    switch(tableIndex)
    {
    case 0:
        ui->IndicateLabel->setText("AI");
        break;
    case 1:
        ui->IndicateLabel->setText("RIOM10输入");
        break;
    case 2:
        ui->IndicateLabel->setText("LCU11输入");
        break;
    case 3:
        ui->IndicateLabel->setText("LCU21输入");
        break;
    case 4:
        ui->IndicateLabel->setText("LCU31输入");
        break;
    case 5:
        ui->IndicateLabel->setText("EVR输入");
        break;
    case 6:
        ui->IndicateLabel->setText("LCU输出");
        break;
    default:
        break;
    }
    ui->currentPage->setText("1");
    ui->totalPage->setText(QString().setNum(totalpage.at(tableIndex)));
    updateTablePage();
}

void PageInterface::on_prevBtn_clicked()
{
    if(ui->currentPage->text().toInt() > 1)
    {
        ui->currentPage->setText(QString().setNum(ui->currentPage->text().toInt() - 1));
        updateTablePage();
    }
}

void PageInterface::on_nextBtn_clicked()
{
    if(ui->currentPage->text().toInt() < ui->totalPage->text().toInt())
    {
        ui->currentPage->setText(QString().setNum(ui->currentPage->text().toInt() + 1));
        updateTablePage();
    }
}

void PageInterface::updateTableValue(int id, int value)
{
    InterfaceInfo info;
    int ret = GlobalConfig::getInterfaceInfo(id, value, info);
    if(ret == -1)
        return;
    if(ret==tableIndex && ui->currentPage->text().toInt()==(info.index/tableRowCount+1) && (info.sys =="AI1" || info.sys =="AO1"))
    {
        tablelist[tableIndex]->item(info.index%tableRowCount, info.car == 1?2:3)->setText(QString().setNum(value));
    }
    else if(ret==tableIndex && ui->currentPage->text().toInt()==(info.index/tableRowCount+1) && info.sys =="AI2")
    {
        tablelist[tableIndex]->item(info.index%tableRowCount, info.car == 1?2:3)->setText(QString().setNum(value));
    }
    else if(ret==tableIndex && ui->currentPage->text().toInt()==(info.index/tableRowCount+1))
        tablelist[tableIndex]->item(info.index%tableRowCount, info.car+1)->setText(QString().setNum(value));
}

void PageInterface::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}
